package com.adtech.common.utils;

import android.text.TextUtils;
import android.util.Log;

/**
 * Descr:   账户信息相关的管理类
 * Author:  bozaixing.
 * Email:   654152983@qq.com.
 * Date:    2017/12/12.
 */

public class AccountManagers {


    private static final String KEY_USER_ID="KEY_USER_ID";
    private static final String KEY_TOKEN = "KEY_TOKEN";
    private static final String KEY_ACCOUNT = "KEY_ACCOUNT";
    private static final String KEY_LOCK = "KEY_GESTURELOCK";
    private static final String KEY_THIRD_TOKEN = "KEY_THIRD_TOKEN";

    // 登录状态的Token，用来接口请求
    private static String token;
    // 登录的账户
    private static String account;
    private static String userId;
    private static String thirdToken;

    /**
     * 存储数据到XML文件，持久化
     */
    private static void save() {
        // 存储数据
        SPUtil.put(KEY_TOKEN, token);
        SPUtil.put(KEY_ACCOUNT, account);
        SPUtil.put(KEY_USER_ID,userId);
        SPUtil.put(KEY_THIRD_TOKEN,thirdToken);
    }

    /**
     * 获取token的值
     *
     * @return
     */
    public static String getToken() {
        token = (String) SPUtil.get(KEY_TOKEN, "");
        Log.e("sign", "token：" + token);
        return token;
    }

    public static String getThirdToken() {
        if(thirdToken==null || "".equals(thirdToken)){
            thirdToken = (String) SPUtil.get(KEY_THIRD_TOKEN, "");
        }
        return thirdToken;
    }

    /**
     * 用户ID
     * @return String
     */
    public static String getUserId() {
        userId = (String) SPUtil.get(KEY_USER_ID, "");
        return userId;
    }

    /**
     * 获取账号
     *
     * @return
     */
    public static String getAccount() {
        account = (String) SPUtil.get(KEY_ACCOUNT, "");
        return account;
    }

    /**
     * 保存用户的登录信息到持久化XML中
     */
    public static void login(String userId,String token, String account,String thirdToken) {
        AccountManagers.userId=userId;
        AccountManagers.token = token;
        AccountManagers.account = account;
        AccountManagers.thirdToken=thirdToken;
        // 保存到本地
        save();
    }

    /**
     * 保存手机号
     *
     * @param account
     */
    public static void bindAccount(String account) {
        SPUtil.put(KEY_ACCOUNT, account);
    }


    /**
     * 保存手势密码
     */
    public static void setGesture(String pwd) {
        SPUtil.put(KEY_LOCK, pwd);
    }

    /**
     * 获取手势密码
     *
     * @return String
     */
    public static String getGesture() {
        return String.valueOf(SPUtil.get(KEY_LOCK, ""));
    }

    /**
     * 返回当前账户是否登录
     *
     * @return True已登录
     */
    public static boolean isLogin() {
        // 用户账号和Token不能为空
        return !TextUtils.isEmpty(getToken());
    }

    /**
     * 实现退出登录的方法
     */
    public static void logOut() {
        SPUtil.remove(KEY_TOKEN);
        SPUtil.remove(KEY_ACCOUNT);
        SPUtil.remove(KEY_LOCK);
        SPUtil.clear();
        token = null;
        account = null;
    }

    public static void clearAll() {
        SPUtil.clear();
        token = null;
        account = null;
    }
}
